| Conditions | 1 |
| Paths | 1 |
| Total Lines | 112 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 16 | function (state, data, visibility, util, format, reactionService, upgradeService) { |
||
| 17 | let ct = this; |
||
| 18 | ct.state = state; |
||
| 19 | ct.data = data; |
||
| 20 | ct.util = util; |
||
| 21 | ct.format = format; |
||
| 22 | ct.upgradeService = upgradeService; |
||
| 23 | ct.adjustAmount = [1, 10, 25, 100]; |
||
| 24 | |||
| 25 | function update(player) { |
||
| 26 | for(let slot of player.element_slots){ |
||
| 27 | if(!slot){ |
||
| 28 | continue; |
||
| 29 | } |
||
| 30 | for (let reaction of slot.reactions) { |
||
| 31 | if (!reaction.active) { |
||
| 32 | continue; |
||
| 33 | } |
||
| 34 | reactionService.react(numberToReact(player, reaction.reaction), reaction.reaction, player); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | function numberToReact(player, reaction) { |
||
| 40 | let power = util.calculateValue(data.global_upgrades.reaction_bandwidth.power.base, |
||
| 41 | data.global_upgrades.reaction_bandwidth.power, |
||
| 42 | player.global_upgrades_current.reaction_bandwidth); |
||
| 43 | let number = power; |
||
| 44 | for(let resource in reaction.reactants){ |
||
| 45 | number = Math.min(number, player.resources[resource].number); |
||
| 46 | } |
||
| 47 | return number; |
||
| 48 | } |
||
| 49 | |||
| 50 | ct.reactionSize = function (player) { |
||
| 51 | let size = 0; |
||
| 52 | for(let slot of player.element_slots){ |
||
| 53 | if(!slot){ |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | size += slot.reactions.length; |
||
| 57 | } |
||
| 58 | return size; |
||
| 59 | }; |
||
| 60 | |||
| 61 | /* Adds a new reaction to the player list */ |
||
| 62 | ct.addReaction = function (player, slot, key) { |
||
| 63 | if(ct.reactionSize(player) >= util.calculateValue(data.global_upgrades.reaction_slots.power.base, |
||
| 64 | data.global_upgrades.reaction_slots.power, |
||
| 65 | player.global_upgrades.reaction_slots)){ |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | let reaction = data.reactions[key]; |
||
| 69 | slot.reactions.push({ |
||
| 70 | active: false, |
||
| 71 | reaction: angular.copy(reaction) |
||
| 72 | }); |
||
| 73 | }; |
||
| 74 | |||
| 75 | ct.removeReaction = function (slot, item) { |
||
| 76 | for(let i = 0; i < slot.reactions.length; i++){ |
||
| 77 | if(slot.reactions[i] === item){ |
||
| 78 | slot.reactions.splice(i, 1); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | }; |
||
| 82 | |||
| 83 | ct.visibleReactions = function(slot) { |
||
| 84 | return slot.reactions; |
||
| 85 | }; |
||
| 86 | |||
| 87 | ct.availableReactions = function(slot, player) { |
||
| 88 | return visibility.visible(data.reactions, isReactionAvailable, slot.element, null, player); |
||
| 89 | }; |
||
| 90 | |||
| 91 | function isReactionAvailable(entry, currentElement, player) { |
||
| 92 | let available = true; |
||
| 93 | let reaction = data.reactions[entry]; |
||
| 94 | for(let resource in reaction.reactant){ |
||
| 95 | available = available && player.resources[resource].unlocked; |
||
| 96 | } |
||
| 97 | // Workaround to reuse the visibility function. It expects an object with the |
||
| 98 | // reaction inside |
||
| 99 | let reactionObject = {reaction:reaction}; |
||
| 100 | return available && isReactionVisible(reactionObject, currentElement); |
||
| 101 | } |
||
| 102 | |||
| 103 | function isReactionVisible(entry, currentElement) { |
||
| 104 | let reaction = entry.reaction; |
||
| 105 | if(reaction.elements.length === 0){ |
||
| 106 | return true; |
||
| 107 | } |
||
| 108 | for(let element of reaction.elements){ |
||
| 109 | if(element === currentElement){ |
||
| 110 | return true; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | ct.adjustLevel = function(player, upgrade, amount){ |
||
| 117 | player.global_upgrades_current[upgrade] += amount; |
||
| 118 | // We cap it between 1 and the current max level |
||
| 119 | player.global_upgrades_current[upgrade] = Math.max(1, Math.min(player.global_upgrades_current[upgrade], player.global_upgrades[upgrade])); |
||
| 120 | }; |
||
| 121 | |||
| 122 | ct.visibleUpgrades = function() { |
||
| 123 | return visibility.visible(data.global_upgrades, upgradeService.filterByTag('reactions')); |
||
| 124 | }; |
||
| 125 | |||
| 126 | state.registerUpdate('reactions', update); |
||
| 127 | }]); |
||
| 128 |